var userUrl = null;

$(function () {

});

var closeEdit = function () {
    $('#dl_user_edit').dialog("close");
}

var addUser = function () {
    $('#form_user_edit').form('clear');
    editUrl = ctx + "/sys/user_add";
    $('#form_user_edit input[name="user.id"]').prop("readonly",false);
    $('#dl_user_edit').dialog("open");
}

var updateUser = function () {
    $('#form_user_edit').form('clear');
    editUrl = ctx + "/sys/user_update";
    var selected = $('#dg_user').datagrid("getSelected");
    if(!selected){
        $.messager.alert('系统提示',"请选择你要修改的用户");
        return;
    }

    $('#form_user_edit input[name="user.id"]').val(selected.id);
    $('#form_user_edit input[name="user.name"]').val(selected.name);
    $('#form_user_edit input[name="user.url"]').val(selected.url);
    $('#form_user_edit input[name="user.parent_id"]').val(selected.parent_id);

    $('#form_user_edit input[name="user.id"]').prop("readonly","readonly");
    $('#dl_user_edit').dialog("open");
}

var submitUser = function () {
    $('#form_user_edit').form('submit',{
        url:editUrl,
        success:function (data) {
            data = eval('('+ data +')');
            if(data && data.msg){
                $.messager.alert('系统提示',data.msg)
            }
            closeEdit();
            $('#dg_user').datagrid("reload");
        }
    });
}

var removeUser = function () {
    var selected = $('#dg_user').datagrid("getSelected");
    if(!selected){
        $.messager.alert('系统提示',"请选择你要删除的用户");
        return;
    }
    var id = selected.id;
    $.messager.confirm('系统提示','你确定要删除用户“'+ selected.name + '”吗?',function (r) {
        if(r){
            $.post(ctx + "/sys/user_delete",{'user.id':id},function (data) {
                if(data && data.msg){
                    $.messager.alert('系统提示',data.msg);
                }
                $('#dg_user').datagrid("reload");
            });
        }
    });
}

var searchUser = function () {
    $('#dg_user').datagrid('load',{
        'user.id': $('#searUserParams input[name="user.id"]').val(),
        'user.name': $('#searUserParams input[name="user.name"]').val(),
        'user.parent_id': $('#searUserParams input[name="user.parent_id"]').val()
    });
}